package com.g2room.search.life;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.g2room.tools.db.db2po.DbConstant;
import com.g2room.utils.StringUtil;

public class IndexFromDb {
	static Index index = new Index();
	static Connection connection = DbUtils.getConn("life_grap");
	public static List<String> tns = new ArrayList<String>();
	static {
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
//		getTableName();''
//		tns.add("life_scenery");
//		tns.add("life_food");
		tns.add("life_arti");
//		tns.add("life_celebrity");
//		tns.add("sch_school");
//		tns.add("sch_comment");
//		tns.add("sch_school_img");
////		tns.add("life_food_do");
////		tns.add("life_food_shop");
//		tns.add("life_farmstay");
////		tns.add("life_news");
////		tns.add("life_district");
//		
//		tns.add("life_driver_school");
////		tns.add("life_scenery");
//		tns.add("com_company");
//		tns.add("com_shop");
////		tns.add("life_subject");
		
		
	}
	public IndexFromDb() {
		DbUtils.closeConn(connection);
		// 获取连接
//		connection = DbUtils.getNewsConn();
	}
	public static void main(String[] args) {
		if(null != args && args.length > 0 && null != args[0]) {
			tns.clear();
			Collections.addAll(tns, args[0].split(";"));
		}
		for(String tn : tns) {
			System.out.println("===========" + tn);
			getDataFromDb(tn);
		}
		DbUtils.closeConn(connection);
	}
	
	public static void getTableName() {  
		try {  
			DatabaseMetaData meta = connection.getMetaData();  
			ResultSet rs = meta.getTables(null, null, null, new String[] { "TABLE" });  
		   	while (rs.next()) {
		   		tns.add(rs.getString(3));
		   	} 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void getDataFromDb(String table) {
		System.out.print(table);
		try {
			Statement statement = connection.createStatement();
			// 分页获取数据
			// 一次取10万
			int size = 100000;
			for(int m = 0; m<100000000; m =m+size) {
				String sql = "select * from " + table + " limit " + m + "," + size;
				System.out.println("==="+sql);
				ResultSet rs = statement.executeQuery(sql);
				
				// 获取列名
				ResultSetMetaData rsm =rs.getMetaData(); //获得列集
				int col = rsm.getColumnCount();   //获得列的个数
				String[] colName = new String[col];
				String[] colType = new String[col];
				//取结果集中的表头名称, 放在colName数组中
				for (int i = 0; i < col; i++) {// 第一列,从1开始.所以获取列名,或列值,都是从1开始
					colName[i] = StringUtil.hump4Underline(rsm.getColumnName( i + 1 ));// 获得列值的方式一:通过其序号
					colType[i] = DbConstant.MAP_JDBC_JAVA_TYPE.get(rsm.getColumnType(i+1));
				}//End for
				rs.beforeFirst();
				List<String> ids = new ArrayList<String>();
				List<String> jsons = new ArrayList<String>();
				if(!rs.next()) return;
				rs.beforeFirst();
			
				int j = 0;
				while(rs.next()) {
					ids.add(rs.getString("id"));
					Map<String, Object> valMap = new HashMap<String, Object>();
					for(int i = 0; i<colName.length; i++) {
						String ct = colType[i];
						if(ct.equals(DbConstant.FIELD_TYPE_INT) 
								|| ct.equals(DbConstant.FIELD_TYPE_BYTE)
								|| ct.equals(DbConstant.FIELD_TYPE_LONG)
								|| ct.equals(DbConstant.FIELD_TYPE_SHORT)
								) {
							valMap.put(colName[i], rs.getInt(i+1));
						} else if(ct.equals(DbConstant.FIELD_TYPE_DOUBLE)
								|| ct.equals(DbConstant.FIELD_TYPE_FLOAT)
								|| ct.equals(DbConstant.FIELD_TYPE_BIGDECIMAL)){
							valMap.put(colName[i], rs.getDouble(i+1));
						} else if(ct.equals(DbConstant.FIELD_TYPE_DATE) || ct.equals(DbConstant.FIELD_TYPE_DATETIME)
								){
////							valMap.put(colName[i], rs.getString(i+1));
//							String d = rs.getString(i+1);
//							if(null != d) valMap.put(colName[i], d.replace(".0", ""));
//						} else if(
//								){
							String d = rs.getString(i+1);
							if(null != d) valMap.put(colName[i], d.replace(".0", ""));
						} else {
							valMap.put(colName[i], rs.getString(i+1));
						}
					}
					jsons.add(JSON.toJSONString(valMap, SerializerFeature.WriteDateUseDateFormat));
					if(j ==0) System.out.print("==开始写入");
					if(j>0 && j%2000 == 0) {
						index.createIndexBatch(ids, jsons, StringUtil.hump4Underline(table));
						ids.clear();
						jsons.clear();
					}
					j++;
				}
				index.createIndexBatch(ids, jsons, StringUtil.hump4Underline(table));
				
				rs.close();
				if(j ==0) {
					System.out.println("==结束写入");
				}
			}
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
			System.out.println(table + "============");
		}
	}
}
